/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bll;

import bean.LoaiHang;
import dal.QuanAoDAL;
import bean.NguoiDung;
import bean.ThongTinNguoiDung;
import java.sql.ResultSet;
import java.util.List;
import helper.Converter;

/**
 *
 * @author HoangNam
 */
public class CategoryBLL {

    private QuanAoDAL dal;
    private Converter conv;

    public CategoryBLL() {
        dal = new QuanAoDAL();
        conv = new Converter();
    }

    public ThongTinNguoiDung getUserInfoByID(int id) {
        String sql = "select * from ThongTinNguoiDung where userid = ?";
        Object[] arr = {id};
        ResultSet rs = dal.executeQuery(sql, arr);
        ThongTinNguoiDung info = (ThongTinNguoiDung) conv.getObject(ThongTinNguoiDung.class, rs);
        return info;
    }

    public boolean insertCategory(LoaiHang categ) {
        String cmd = "insert into LoaiHang values(?, ?, ?, ?)";
        Object[] arr = {categ.getTenLoai(), categ.getQuyCach(), categ.getGhiChu(), categ.getTrangThai()};
        return dal.executeUpdate(cmd, arr);
    }

    public boolean updateCategory(LoaiHang categ) {
        String cmd = "update LoaiHang set tenloai=?, quycach=?, ghichu=?, trangthai=? where loaihangid=?";
        Object[] arr = {categ.getTenLoai(), categ.getQuyCach(), categ.getGhiChu(), categ.getTrangThai(), categ.getLoaiHangID()};
        return dal.executeUpdate(cmd, arr);
    }

    public List<LoaiHang> searchCategory(String keyword) {
        String cmd = "select * from LoaiHang where tenloai like ? order by tenloai";
        Object[] arr = {"%" + keyword + "%"};
        ResultSet rs = dal.executeQuery(cmd, arr);
        List<LoaiHang> list = conv.getObjectList(LoaiHang.class, rs);
        return list;
    }

    public static void main(String[] args) {
        CategoryBLL catgBLL = new CategoryBLL();
        List<LoaiHang> list = catgBLL.searchCategory("");
        for (LoaiHang l : list) {
            System.out.println(l);
        }
    }

    public boolean deleteCategory(int loaiHangID) {
        String cmd = "delete from LoaiHang where loaihangid=?";
        Object[] arr = {loaiHangID};
        return dal.executeUpdate(cmd, arr);
    }
}
